home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / hobby / ast44src.zip / PLACALC.C < prev    next >
C/C++ Source or Header  |  1995-02-11  |  47KB  |  1,341 lines

  1. /*
  2. ** Astrolog (Version 4.40) File: placalc.c
  3. **
  4. ** IMPORTANT NOTICE: The graphics database and chart display routines
  5. ** used in this program are Copyright (C) 1991-1995 by Walter D. Pullen
  6. ** (astara@u.washington.edu). Permission is granted to freely use and
  7. ** distribute these routines provided one doesn't sell, restrict, or
  8. ** profit from them in any way. Modification is allowed provided these
  9. ** notices remain with any altered or edited versions of the program.
  10. **
  11. ** The main planetary calculation routines used in this program have
  12. ** been Copyrighted and the core of this program is basically a
  13. ** conversion to C of the routines created by James Neely as listed in
  14. ** Michael Erlewine's 'Manual of Computer Programming for Astrologers',
  15. ** available from Matrix Software. The copyright gives us permission to
  16. ** use the routines for personal use but not to sell them or profit from
  17. ** them in any way.
  18. **
  19. ** The PostScript code within the core graphics routines are programmed
  20. ** and Copyright (C) 1992-1993 by Brian D. Willoughby
  21. ** (brianw@sounds.wa.com). Conditions are identical to those above.
  22. **
  23. ** The extended accurate ephemeris databases and formulas are from the
  24. ** calculation routines in the program "Placalc" and are programmed and
  25. ** Copyright (C) 1989,1991,1993 by Astrodienst AG and Alois Treindl
  26. ** (alois@azur.ch). The use of that source code is subject to
  27. ** regulations made by Astrodienst Zurich, and the code is not in the
  28. ** public domain. This copyright notice must not be changed or removed
  29. ** by any user of this program.
  30. **
  31. ** Initial programming 8/28,30, 9/10,13,16,20,23, 10/3,6,7, 11/7,10,21/1991.
  32. ** X Window graphics initially programmed 10/23-29/1991.
  33. ** PostScript graphics initially programmed 11/29-30/1992.
  34. ** Last code change made 1/29/1995.
  35. */
  36.  
  37. #include "placalc.h"
  38.  
  39.  
  40. #ifdef PLACALC
  41. /*
  42. ** ---------------------------------------------------------------
  43. ** | Copyright Astrodienst AG and Alois Treindl, 1989,1991,1993  |
  44. ** | The use of this source code is subject to regulations made  |
  45. ** | by Astrodienst Zurich. The code is NOT in the public domain.|
  46. ** |                                                             |
  47. ** | This copyright notice must not be changed or removed        |
  48. ** | by any user of this program.                                |
  49. ** ---------------------------------------------------------------
  50. **
  51. ** Important changes:
  52. ** 11-jun-93 revision 1.12: fixed error which affected Mercury between -2100
  53. ** and -3100 (it jumped wildly).
  54. */
  55.  
  56. /************************************************************
  57. externally accessible globals, defined as extern in placalc.h
  58. ************************************************************/
  59.  
  60. REAL8 meanekl, ekl, nut;
  61. struct elements el[MARS + 1];
  62.  
  63. /*
  64. ** The global variable ephe_path indicates where the ephemeris files
  65. ** LRZ5_xx and CHI_xx are stored.
  66. ** By default it is set to the #defined EPHE_PATH, but the user of the
  67. ** placalc module can change it to any other location before he
  68. ** starts calling calc().
  69. */
  70.  
  71. char *ephe_path = EPHE_PATH;
  72.  
  73.  
  74. #ifdef ASTROLOG
  75. /* Given an object index and a Julian Day time, get its zodiac and        */
  76. /* declination position (planetary longitude and latitude) of the object  */
  77. /* and its velocity and distance from the Earth or Sun. This basically    */
  78. /* just calls the Placalc calculation function to actually do it, but as  */
  79. /* this is the one routine called from Astrolog, this is the one routine  */
  80. /* which has knowledge of and uses both Astrolog and Placalc definitions, */
  81. /* and does things such as translation to Placalc indices and formats.    */
  82.  
  83. bool FPlacalcPlanet(ind, jd, helio, planet, planetalt, ret, space)
  84. int ind, helio;
  85. double jd;
  86. real *planet, *planetalt, *ret, *space;
  87. {
  88.   int iplanet, flag;
  89.   REAL8 jd_ad, rlng, rrad, rlat, rspeed;
  90.  
  91.   if (ind <= oPlu)      /* Convert Astrolog object index to Placalc index. */
  92.     iplanet = ind-1;
  93.   else if (ind == oChi)
  94.     iplanet = CHIRON;
  95.   else if (ind == oNod)
  96.     iplanet = us.fTrueNode ? TRUE_NODE : MEAN_NODE;
  97.   else if (ind == oLil)
  98.     iplanet = LILITH;
  99.   else
  100.     return fFalse;
  101.  
  102.   jd_ad = jd - JUL_OFFSET;
  103.   flag = helio ? CALC_BIT_SPEED | CALC_BIT_HELIO : CALC_BIT_SPEED;
  104.   jd_ad += deltat(jd_ad);
  105.   if (calc(iplanet, jd_ad, flag, &rlng, &rrad, &rlat, &rspeed) == OK) {
  106.     *planet    = rlng;
  107.     *planetalt = rlat;
  108.     *ret       = rspeed;
  109.     *space     = rrad;
  110.     return fTrue;
  111.   }
  112.   return fFalse;
  113. }
  114. #endif /* ASTROLOG */
  115.  
  116.  
  117. /* function calc(): 
  118. ** This is the main routine for computing a planets position.
  119. ** The function has several modes, which are controlled by bits in
  120. ** the parameter 'flag'. The normal mode (flag == 0) computes
  121. ** a planets apparent geocentric position in ecliptic coordinates relative to
  122. ** the true equinox of date, without speed
  123. ** 
  124. ** Explanation of the arguments: see the functions header.
  125. ** 
  126. ** Returns OK or ERR (if some planet out of time range). OK and ERR are
  127. ** defined in ourdef.h and must not be confused with TRUE and FALSE.
  128. ** OK and ERR are of type int, not of type BOOLEAN.
  129. ** 
  130. ** Bits used in flag:
  131. ** CALC_BIT_HELIO     0 = geocentric, 1 = heliocentric
  132. ** CALC_BIT_NOAPP       0 = apparent positions, 1 = true positions
  133. ** CALC_BIT_NONUT     0 = do nutation (true equinox of date)
  134. ** 1 = don't do nutation (mean equinox of date).
  135. ** 
  136. ** CALC_BIT_SPEED     0 = don't calc speed,
  137. ** 1 = calc speed, takes quite long for moon
  138. ** (is observed only for moon, with other
  139. ** planets speed is cheap)
  140. ** 
  141. ** Side effects and local memory:
  142. ** For doing heliocentric positions the fucntion must know the
  143. ** earth's position for the desired time t. It remembers the earth
  144. ** position so it does not have to recompute it each time a planet
  145. ** position is wanted for the same time t.
  146. ** It calls helup(t), which leaves as a side effect the global
  147. ** variables meanekl, ekl and nut for the time t.
  148. ** 
  149. ** Functions called by calc():
  150. ** helup(t)
  151. ** hel(t)
  152. ** moon(t)
  153. ** togeo()
  154. ** 
  155. ** Time range:
  156. ** The function can be used savely in the time range 5000 BC to
  157. ** 3000 AD. The stored ephemeris is available only for this time
  158. ** range, so Jupiter ... Pluto cannot be computed outside. The
  159. ** function will return results for the other planets also outside
  160. ** of this time range, but they become meaningless pretty soon
  161. ** before 5000 BC, because Newcombs time series expansions for the
  162. ** elements will not work anymore.
  163. ** 
  164. ** pointers to the return variables:
  165. ** alng = ecliptic longitude in degrees
  166. ** arad = radius vector in AU (astronomic units)
  167. ** alat = ecliptic latitude in degrees
  168. ** alngspeed = speed of planet in degrees per day
  169. ** 
  170. ** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  171. ** The precision of the speed is quite limited.
  172. ** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  173. ** 
  174. ** For Sun, Mercury, Venus and Mars we take only the speed from
  175. ** the undisturbed Kepler orbit. For the Moon there is no
  176. ** reasonable undisturbed orbit and we derive the speed from
  177. ** its position at t + dt and t - dt. We need these 
  178. ** moon positions anyway for the true node calculation.
  179. ** For the outer planets and Chiron we derive the precise
  180. ** speed from the stored ephemeris by high order inter-
  181. ** polation; the precision is limited for the geocentric
  182. ** case due to the limited precision of the earth's/sun's speed.
  183. ** Applications who need precise speeds should
  184. ** get them by calling calc() with slightly different times.
  185. ** 
  186. ** Comment 7 May 1991 by Alois Treindl:
  187. ** Center of Earth versus Barycenter Earth-Moon:
  188. ** Brown's theory of the moon gives the moon's coordinates relative
  189. ** to the center of the earth. Newcomb's theory of the Sun gives the
  190. ** coordinates of the earth's center relative to the center of the Sun.
  191. ** This is what we need.
  192. **
  193. ** How about the Mean Lunar Node?
  194. ** The orbital elements of the Sun in Newcomb's theory are given
  195. ** relative to the barycenter Earth-Moon; the reduction to geocentric
  196. ** is only applied after doing the Kepler ellipse calculation.
  197. ** Are the Lunar elements also relative to the barycenter??
  198. ** If yes:
  199. ** When we use the moon's mean node out of the elements, it is still
  200. ** as seen from the barycenter. Because the node is close to the
  201. ** earth, we would have to apply a considerable correction, which is of
  202. ** the order of 4000/384000 km or 35' (minutes of arc).
  203. ** Nobody has ever applied such a correction to the mean node.
  204. **
  205. ** And the True Node?
  206. ** When we calculate the osculating orbital elements of the Moon (true node),
  207. ** are they relative to the barycenter or to the Earth's center?
  208. ** Our derivation of true node from the actual Moon positions considers
  209. ** the earth's center as the focal point of the osculating lunar ellipse.
  210. ** A more correct approach would first reduce the lunar position from
  211. ** geocentric to barycentric, then compute the orbital elements from
  212. ** the reduced positions, and then reduce the desired items
  213. ** (node, apogaeum, 'dark moon') to geocentric positions.
  214. ** No known astrological ephemeris has ever used such a correction, which is
  215. ** of the same order of magnitude as the correction to the meannode above.
  216. ** When the moon is going through the ecliptic, the geocenter, barycenter
  217. ** moon (and the node identical to the moon itself) line up; this is why
  218. ** the error does not show up in normal considerations.
  219. */
  220.  
  221. int calc(planet, jd_ad, flag, alng, arad, alat, alngspeed)
  222. int planet;  /* planet index as defined in placalc.h,
  223. SUN = 0, MOON = 1 etc.
  224. planet == -1 calc calculates only nut and ecl */
  225. REAL8 jd_ad;  /* relative Astrodienst Juldate, ephemeris time.
  226. Astrodienst Juldate is relative 31 Dec 1949, noon. */
  227. int flag;  /* See definition of flag bits above */
  228. REAL8 *alng;
  229. REAL8 *arad;
  230. REAL8 *alat;
  231. REAL8 *alngspeed;
  232. {
  233.   struct rememberdat  /* time for which the datas are calculated */
  234.     {REAL8 calculation_time, lng, rad, zet, lngspeed, radspeed, zetspeed;};
  235.   static struct rememberdat earthrem = 
  236.     {HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8};
  237.   static struct rememberdat moonrem  = 
  238.     {HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8};
  239.   REAL8 c, s, x, knn, knv; 
  240.   REAL8 rp, zp; /* needed to call hel! */
  241.   REAL8 *azet = alat; 
  242.   BOOLEAN calc_geo, calc_helio, calc_apparent, calc_speed,
  243.   calc_nut;
  244.  
  245.   /* helup checks whether it was already called with same time */
  246.   helup (jd_ad);
  247.   if (planet == CALC_ONLY_ECL_NUT)
  248.     return OK;
  249.  
  250.   calc_helio =  flag & CALC_BIT_HELIO;
  251.   calc_geo = ! calc_helio;
  252.   calc_apparent = ! (flag & CALC_BIT_NOAPP);
  253.   calc_nut = ! (flag & CALC_BIT_NONUT);
  254.   calc_speed = flag & CALC_BIT_SPEED;
  255.   /*
  256.   ** it is necessary to compute EARTH in the following cases:
  257.   ** heliocentric MOON or EARTH
  258.   ** geocentric any planet except MOON or nodes or LILITH
  259.   */
  260.   if (calc_helio && (planet == MOON || planet == EARTH)
  261.     || calc_geo && planet != MOON
  262.     && planet != MEAN_NODE
  263.     && planet != TRUE_NODE
  264.     && planet != LILITH) {
  265.     if (earthrem.calculation_time != jd_ad) {  
  266.       hel (EARTH, jd_ad, alng, arad, azet, alngspeed, &rp, &zp);
  267.       /* store earthdata for geocentric calculation: */
  268.       earthrem.lng = *alng;  
  269.       earthrem.rad = *arad;
  270.       earthrem.zet = *azet;
  271.       earthrem.lngspeed = *alngspeed;
  272.       earthrem.radspeed = rp;
  273.       earthrem.zetspeed = zp;
  274.       earthrem.calculation_time = jd_ad;
  275.     }
  276.   }
  277.   switch(planet) {
  278.  
  279.   case EARTH: /* has been already computed */
  280.     *alng = earthrem.lng;
  281.     *arad = earthrem.rad;
  282.     *azet = earthrem.zet;
  283.     *alngspeed = earthrem.lngspeed;
  284.     rp = earthrem.radspeed;
  285.     zp = earthrem.zetspeed;
  286.     if (calc_geo) { /* SUN seen from earth */
  287.       *alng = smod8360(*alng + 180.0);  
  288.       *azet = - *azet;
  289.     }
  290.     if (calc_apparent)
  291.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  292.     break; 
  293.  
  294.   case MOON:
  295.     moon(alng, arad, azet);  
  296.     moonrem.lng = *alng;  /* moonrem will be used for TRUE_NODE */
  297.     moonrem.rad = *arad;
  298.     moonrem.zet = *azet;
  299.     *alngspeed = 12;
  300.     moonrem.calculation_time = jd_ad;
  301.     if (calc_helio || calc_speed) {/* get a second moon position */
  302.       REAL8 lng2, rad2, zet2;
  303.       helup(jd_ad + MOON_SPEED_INTERVAL);   
  304.       moon(&lng2, &rad2, &zet2);  
  305.       helup(jd_ad);
  306.       if (calc_helio) { /* moon as seen from sun */
  307.         togeo(earthrem.lng, -earthrem.rad, moonrem.lng, moonrem.rad,
  308.         moonrem.zet, alng, arad);
  309.         togeo(earthrem.lng + MOON_SPEED_INTERVAL * earthrem.lngspeed, 
  310.         -(earthrem.rad + MOON_SPEED_INTERVAL * earthrem.radspeed), 
  311.         lng2, rad2, zet2, &lng2, &rad2);
  312.       }
  313.       *alngspeed =  diff8360(lng2, *alng) / MOON_SPEED_INTERVAL;
  314.       /* rp = (rad2 - *arad) / MOON_SPEED_INTERVAL;       */
  315.       /* zp = (zet2 - moonrem.zet) / MOON_SPEED_INTERVAL; */
  316.     }
  317.     *alat = RADTODEG * ASIN8(*azet / *arad);
  318.     /*
  319.     ** light time correction, not applied for moon or nodes;
  320.     ** moon would have only term of ca. 0.02", see Expl.Sup.1961 p.109
  321.     */
  322.     break;
  323.  
  324.   case MERCURY:
  325.   case VENUS:
  326.   case MARS:
  327.   case JUPITER:
  328.   case SATURN:
  329.   case URANUS:
  330.   case NEPTUNE:
  331.   case PLUTO:
  332.   case CHIRON:
  333.     if (hel (planet, jd_ad, alng, arad, azet, alngspeed, &rp, &zp) != OK)
  334.       return ERR; /* outer planets can fail if out of ephemeris range */
  335.     if (calc_geo) {       /* geocentric */
  336.       REAL8 lng1, rad1, lng2, rad2;
  337.       togeo(earthrem.lng, earthrem.rad, *alng, *arad, *azet, &lng1, &rad1);
  338.       togeo(earthrem.lng + earthrem.lngspeed, 
  339.       earthrem.rad + earthrem.radspeed, 
  340.       *alng + *alngspeed, *arad + rp, *azet + zp, &lng2, &rad2); 
  341.       *alng = lng1;
  342.       *arad = rad1; 
  343.       *alngspeed = diff8360(lng2, lng1);
  344.       /* rp = rad2 - rad1; */
  345.     }
  346.     *alat = RADTODEG * ASIN8(*azet / *arad);
  347.     if (calc_apparent)
  348.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  349.     break;
  350.  
  351.   case MEAN_NODE:
  352.     *alng = smod8360(el[MOON].kn);
  353.     /*
  354.     * the distance of the node is the 'orbital parameter' p = a (1-e^2);
  355.     * Our current use of the axis a is wrong, but is never used.
  356.     */
  357.     *arad = pd[MOON].axis;
  358.     *alat = 0.0;
  359.     *alngspeed = -0.053;
  360.     break;
  361.  
  362.   case TRUE_NODE: {
  363.     /* see comment 'Note 7 May 1991' above */
  364.     REAL8 ln, rn, zn, 
  365.     lv, rv, zv, 
  366.     l1, r1, z1, 
  367.     xn, yn, xv, yv, r0, x0, y0;
  368.  
  369.     helup(jd_ad + NODE_INTERVAL);
  370.     moon(&ln, &rn, &zn);
  371.     helup(jd_ad - NODE_INTERVAL);
  372.     moon(&lv, &rv, &zv);
  373.     helup(jd_ad);
  374.     if (moonrem.calculation_time != jd_ad) 
  375.       moon(&l1, &r1, &z1);  
  376.     else {  /* moon is already calculated */
  377.       l1 = moonrem.lng;
  378.       r1 = moonrem.rad;
  379.       z1 = moonrem.zet;
  380.     }
  381.     rn = sqrt(rn * rn - zn * zn);
  382.     rv = sqrt(rv * rv - zv * zv);
  383.     r0 = sqrt(r1 * r1 - z1 * z1);
  384.     xn = rn * COS8(DEGTORAD * ln);
  385.     yn = rn * SIN8(DEGTORAD * ln);
  386.     xv = rv * COS8(DEGTORAD * lv);
  387.     yv = rv * SIN8(DEGTORAD * lv);
  388.     x0 = r0 * COS8(DEGTORAD * l1);   
  389.     y0 = r0 * SIN8(DEGTORAD * l1);   
  390.     x = test_near_zero(x0 * yn - xn * y0);
  391.     s = (y0 * zn - z1 * yn) / x;
  392.     c = test_near_zero((x0 * zn - z1 * xn) / x);
  393.     knn =  smod8360(RADTODEG * ATAN28(s, c)); /* = ATAN8(s / c) */
  394.     x = test_near_zero(y0 * xv - x0 * yv);
  395.     s = (yv * z1 - zv * y0) / x;
  396.     c = test_near_zero((xv * z1 - zv * x0) / x);
  397.     knv =  smod8360(RADTODEG * ATAN28(s, c));        
  398.     *alng = smod8360((knv + knn) / 2);
  399.     /*
  400.     ** the distance of the node is the 'orbital parameter' p = a (1-e^2);
  401.     ** Our current use of the axis a is wrong.
  402.     */
  403.     *arad = pd[MOON].axis;  
  404.     *alat = 0.0;
  405.     *alngspeed = diff8360(knn, knv) / NODE_INTERVAL;
  406.     }
  407.     break;
  408.  
  409.   case LILITH: {
  410.     /*
  411.     ** Added 22-Jun-93
  412.     ** Lilith or Dark Moon is the empty focal point of the mean lunar ellipse.
  413.     ** This is 180 degrees from the perihel.
  414.     ** Because the lunar orbit is not in the ecliptic, it must be
  415.     ** projected onto the ecliptic in the same way as the planetary orbits
  416.     ** are (see for example Montenbruck, Grundlagen der Ephemeridenrechnung).
  417.     **
  418.     ** We compute the MEAN Lilith, not the TRUE one which would have to be
  419.     ** derived in a similar way as the true node.
  420.     ** For the radius vector of Lilith we use a simple formula; 
  421.     ** to get a precise value, the fact that the focal point of the ellipse
  422.     ** is not at the center of the earth but at the barycenter moon-earth
  423.     ** would have to be accounted for.
  424.     ** For the speed we always return a constant: the T term from the
  425.     ** lunar perihel.
  426.     ** Joelle de Gravelaine publishes in her book "Lilith der schwarze Mond"
  427.     ** (Astrodata, 1990) an ephemeris which gives noon (12.00) positions
  428.     ** but does not project onto the ecliptic.
  429.     ** This creates deviations 
  430.     */
  431.     double arg_lat, lon, cosi;
  432.     struct elements *e = &el[MOON];
  433.     arg_lat = degnorm(e->pe - e->kn + 180.0);
  434.     cosi = COSDEG(e->in);
  435.     if (e->in == 0 || ABS8(arg_lat -  90.0) < TANERRLIMIT 
  436.       || ABS8(arg_lat - 270.0) < TANERRLIMIT) {
  437.       lon = arg_lat;
  438.     } else {
  439.       lon = ATAN8(TANDEG(arg_lat) * cosi);
  440.       lon = RADTODEG * lon;
  441.       if (arg_lat > 90.0 && arg_lat < 270.0)  lon += 180.0;
  442.     }
  443.     lon = degnorm(lon + e->kn);
  444.     *alng = lon;
  445.     *alngspeed = 0.111404;  /* 6'41.05" per day */
  446.     *arad = 2 * pd[MOON].axis * e->ex;
  447.     /*
  448.     ** To test Gravalaines error, return unprojected long in alat.
  449.     ** the correct latitude would be:
  450.     ** *alat = RADTODEG * ASIN8(SINDEG(arg_lat) * SINDEG(e->in));
  451.     */
  452. #ifdef ASTROLOG
  453.     *alat = RADTODEG * ASIN8(SINDEG(arg_lat) * SINDEG(e->in));
  454. #else
  455.     *alat = degnorm(arg_lat + e->kn); /* unprojected longitude, no nut */
  456. #endif
  457.     }
  458.     break;
  459.  
  460.   default:
  461.     return ERR;
  462.   } /* end switch */ 
  463.  
  464.   if (calc_nut)
  465.     *alng += nut;
  466.   *alng = smod8360(*alng);  /* normalize to circle */
  467.   return OK;
  468. }
  469.  
  470.  
  471. /*
  472. ** get relative earth distance in range 0..1000:
  473. ** To compute the relative distance we use fixed values of 
  474. ** mimimum and maximum distance measured empirically between
  475. ** 1300 AD and 2300 AD (see helconst.c). 
  476. ** This approach is certainly fine for the
  477. ** outer planets, but not the best for Sun and Moon, where it
  478. ** would be better to look at the mean anomaly, i.e. the progress
  479. ** the planet makes on it's Kepler orbit.
  480. ** Considering the low importance astrologers give to the relative
  481. ** distance, we consider the effort not worthwile.
  482. ** Now we compare real radius with longtime-averaged distances.
  483. */
  484.  
  485. int rel_geo(planet, rau)
  486. int planet;
  487. double rau;
  488. {
  489.   int rgeo;
  490.  
  491.   if (planet == MEAN_NODE || planet == TRUE_NODE || planet == LILITH) {
  492.     return 0;
  493.   } else {
  494.     rgeo = 1000 * (int)(1.0 - (rau - rmima[planet][0]) /
  495.       (rmima[planet][1] - rmima[planet][0]));
  496.   }
  497.   if (rgeo < 0)
  498.     rgeo = 0;
  499.   else if (rgeo > 999)
  500.     rgeo = 999;
  501.   return rgeo;
  502. }
  503.  
  504.  
  505. /* helio to geocentric conversion */
  506.  
  507. void togeo(lngearth, radearth, lng, rad, zet, alnggeo, aradgeo)
  508. REAL8 lngearth;
  509. REAL8 radearth;
  510. REAL8 lng;
  511. REAL8 rad;
  512. REAL8 zet;
  513. REAL8 *alnggeo;
  514. REAL8 *aradgeo;
  515. {
  516.   REAL8 r1, x, y;
  517.  
  518.   r1 = sqrt(rad * rad - zet * zet);
  519.   x = r1 * COS8(DEGTORAD * lng) - radearth * COS8(DEGTORAD * lngearth);
  520.   y = r1 * SIN8(DEGTORAD * lng) - radearth * SIN8(DEGTORAD * lngearth);
  521.   *aradgeo = sqrt(x * x + y * y + zet * zet);
  522.   x = test_near_zero(x);
  523.   *alnggeo = smod8360(RADTODEG * ATAN28(y, x));
  524. }
  525.  
  526.  
  527. /*
  528. ** helup()
  529. ** prepares the orbital elements and the disturbation arguments for the
  530. ** inner planets and the moon. helup(t) is called by hel() and by calc().
  531. ** helup() returns its results in global variables.
  532. ** helup() remembers the t it has been called with before and does
  533. ** not recalculate its results when it is called more than once with
  534. ** the same t.
  535. */
  536.  
  537. void helup(jd_ad)
  538. REAL8 jd_ad;
  539. {
  540.   int i;
  541.   static REAL8 thelup = HUGE8;  /* is initialized only once at load time */
  542.   struct elements *e = el;      /* pointer to el[i] */  
  543.   struct elements *ee = el;     /* pointer to el[EARTH] */  
  544.   struct eledata  *d = pd;      /* pointer to pd[i] */
  545.   REAL8 td, ti, ti2, tj1, tj2, tj3;
  546.  
  547.   if (thelup == jd_ad)
  548.     return; /* if already calculated then return */
  549.  
  550.   for (i = SUN; i <= MARS; i++, d++, e++) {
  551.     td = jd_ad - d->epoch;
  552.     ti = e->tj = td / 36525.0;  /* julian centuries from epoch */
  553.     ti2 = ti * ti;
  554.     tj1 = ti / 3600.0;  /* used when coefficients are in seconds of arc */
  555.     tj2 = ti * tj1;
  556.     tj3 = ti * tj2;
  557.     e->lg = mod8360(d->lg0 + d->lg1 * td  + d->lg2 * tj2 + d->lg3 * tj3);
  558.     /* also with moon lg1 *td is exact to 10e-8 degrees within 5000 years */
  559.     e->pe = mod8360(d->pe0 + d->pe1 * tj1 + d->pe2 * tj2 + d->pe3 * tj3);
  560.     e->ex = d->ex0 + d->ex1 * ti + d->ex2 * ti2;
  561.     e->kn = mod8360(d->kn0 + d->kn1 * tj1 + d->kn2 * tj2 + d->kn3 * tj3);
  562.     e->in = d->in0 + d->in1 * tj1 + d->in2 * tj2;
  563.     e->ma = smod8360(e->lg - e->pe);
  564.  
  565.     if (i == MOON) {
  566.       /* calculate ekliptic according Newcomb, APAE VI,
  567.       ** and nutation according Exp.Suppl. 1961, identical
  568.       ** with Mark Potttenger elemnut()
  569.       ** all terms >= 0.01" only .
  570.       ** The 1984 IAU Theory of Nutation, as published in
  571.       ** AE 1984 suppl. has not yet been implemented
  572.       ** because it would mean to use other elements of
  573.       ** moon and sun */
  574.  
  575.       REAL8 mnode, mlong2, slong2, mg, sg, d2;
  576.  
  577.       mnode  = DEGTORAD * e->kn;        /* moon's mean node */
  578.       mlong2 = DEGTORAD * 2.0 * e->lg;  /* 2 x moon's mean longitude */
  579.       mg     = DEGTORAD * e->ma;        /* moon's mean anomaly (g1) */
  580.       slong2 = DEGTORAD * 2.0 * ee->lg; /* 2 x sun's mean longitude (L), with
  581.                                         the phase 180 deg earth-sun irrelevant
  582.                                         because 2 x 180 = 360 deg */
  583.       sg     = DEGTORAD * ee->ma; /* sun's mean anomaly = earth's */
  584.       d2     = mlong2 - slong2;   /* 2 x elongation of moon from sun */
  585.       meanekl = ekld[0] + ekld[1] * tj1 + ekld[2] * tj2 + ekld[3] * tj3;
  586.       ekl = meanekl +
  587.         (9.2100 * COS8(mnode)
  588.         - 0.0904 * COS8(2.0 * mnode)
  589.         + 0.0183 * COS8(mlong2 - mnode)
  590.         + 0.0884 * COS8(mlong2)
  591.         + 0.0113 * COS8(mlong2 + mg)
  592.         + 0.5522 * COS8(slong2)
  593.         + 0.0216 * COS8(slong2 + sg)) / 3600.0;
  594.         nut = ((-17.2327 - 0.01737 * ti) * SIN8(mnode)
  595.         + 0.2088 * SIN8(2.0 * mnode)
  596.         + 0.0675 * SIN8(mg)
  597.         - 0.0149 * SIN8(mg - d2)
  598.         - 0.0342 * SIN8(mlong2 - mnode)
  599.         + 0.0114 * SIN8(mlong2 - mg)
  600.         - 0.2037 * SIN8(mlong2)
  601.         - 0.0261 * SIN8(mlong2 + mg)
  602.         + 0.0124 * SIN8(slong2 - mnode)
  603.         + 0.0214 * SIN8(slong2 - sg)
  604.         - 1.2729 * SIN8(slong2)
  605.         - 0.0497 * SIN8(slong2 + sg)
  606.         + 0.1261 * SIN8(sg)) / 3600.0;
  607.     }
  608.   }
  609.  
  610.   /* calculate the arguments sa[] for the disturbation terms */ 
  611.   ti = (jd_ad - EPOCH1850) / 365.25;  /* julian years from 1850 */
  612.   for (i = 0; i < SDNUM; i++) 
  613.     sa [i] = mod8360(_sd [i].sd0 + _sd [i].sd1 * ti);
  614.   /*
  615.   ** sa[2] += 0.3315 * SIN8 (DEGTORAD *(133.9099 + 38.39365 * el[SUN].tj));
  616.   **
  617.   ** correction of jupiter perturbation argument for sun from Pottenger;
  618.   ** creates only .03" and 1e-7 rad, not applied because origin unclear */
  619.   thelup = jd_ad;               /* note the last helup time */
  620. }
  621.  
  622.  
  623. /*
  624. ** hel()
  625. ** Computes the heliocentric positions for all planets except the moon.
  626. ** The outer planets from Jupiter onwards, including Chiron, are 
  627. ** actually done by a subsequent call to outer_hel() which takes
  628. ** exactly the same parameters.
  629. ** hel() does true position relative to the mean ecliptic and equinox
  630. ** of date. Nutation is not added and must be done so by the caller.
  631. ** The latitude of the Sun (max. 0.5") is neglected and always returned
  632. ** as zero.
  633. ** 
  634. ** return: OK or ERR
  635. */
  636.  
  637. int hel(planet, t, al, ar, az, alp, arp, azp)
  638. int planet;   /* planet index as defined by placalc.h */
  639. REAL8 t;      /* relative juliand date, ephemeris time */
  640.               /* Now come 6 pointers to return values. */
  641. REAL8 *al;    /* longitude in degrees */
  642. REAL8 *ar;    /* radius in AU */
  643. REAL8 *az;    /* distance from ecliptic in AU */
  644. REAL8 *alp;   /* speed in longitude, degrees per day */
  645. REAL8 *arp;   /* speed in radius, AU per day */
  646. REAL8 *azp;   /* speed in z, AU per day */
  647. {
  648.   register struct elements *e;
  649.   register struct eledata  *d;
  650.   REAL8 lk = 0.0;
  651.   REAL8 rk = 0.0;
  652.   REAL8 b, h1, sini, sinv, cosi, cosu, cosv, man, truanom, esquare, 
  653.     k8, u, up, v, vp;
  654.  
  655.   if (planet >= JUPITER) 
  656.     return (outer_hel(planet, t, al, ar, az, alp, arp, azp));
  657.   if (planet < SUN || planet == MOON)
  658.     return ERR;
  659.  
  660.   e = &el[planet];
  661.   d = &pd[planet];
  662.   sini = SIN8(DEGTORAD * e->in);
  663.   cosi = COS8(DEGTORAD * e->in);
  664.   esquare = sqrt((1.0 + e->ex) / (1.0 - e->ex)); /* H6 in old version */
  665.   man = e->ma;
  666.   if (planet == EARTH)  /* some longperiodic terms in mean longitude */
  667.     man += (0.266 * SIN8 (DEGTORAD * (31.8 + 119.0 * e->tj))
  668.       + 6.40 * SIN8(DEGTORAD * (231.19 + 20.2 * e->tj))
  669.       + (1.882-0.016*e->tj) * SIN8(DEGTORAD * (57.24 + 150.27 * e->tj))
  670.       ) / 3600.0;
  671.   if (planet == MARS)  /* some longperiodic terms */
  672.     man += (0.606 * SIN8(DEGTORAD * (212.87 + e->tj * 119.051))
  673.       + 52.490 * SIN8(DEGTORAD * (47.48 + e->tj * 19.771))
  674.       +  0.319 * SIN8(DEGTORAD * (116.88 + e->tj * 773.444))
  675.       +  0.130 * SIN8(DEGTORAD * (74 + e->tj * 163))
  676.       +  0.280 * SIN8(DEGTORAD * (300 + e->tj * 40.8))
  677.       -  (37.05 +13.5 * e->tj)
  678.       ) / 3600.0;
  679.   u = fnu (man, e->ex, 0.0000003); /* error 0.001" returns radians */  
  680.   cosu = COS8(u);
  681.   h1 = 1 - e->ex * cosu;
  682.   *ar = d->axis * h1;
  683.   if (ABS8(rPi - u) < TANERRLIMIT)
  684.     truanom = u; /* very close to aphel */
  685.   else
  686.     truanom = 2.0 * ATAN8(esquare * TAN8(u * 0.5)); /* true anomaly, rad*/
  687.   v = smod8360(truanom * RADTODEG + e->pe - e->kn); /* argument of latitude */
  688.   if (sini == 0.0 || ABS8(v -  90.0) < TANERRLIMIT 
  689.     || ABS8(v - 270.0) < TANERRLIMIT) {
  690.     *al = v;
  691.   } else {
  692.     *al = RADTODEG * ATAN8(TAN8(v * DEGTORAD) * cosi);
  693.     if (v > 90.0 && v < 270.0)  *al += 180.0;
  694.   }
  695.   *al = smod8360(*al + e->kn);
  696.   sinv = SIN8(v * DEGTORAD);
  697.   cosv = COS8(v * DEGTORAD);
  698.   *az = *ar * sinv * sini;
  699.   b = ASIN8(sinv * sini);     /* latitude in radians */
  700.   k8 = cosv / COS8(b) * sini;
  701.   up = 360.0 / d->period / h1;    /* du/dt degrees/day */
  702.   if (ABS8(rPi - u) < TANERRLIMIT)
  703.     vp = up / esquare;  /* speed at aphel */
  704.   else
  705.     vp = up * esquare * (1 + COS8 (truanom)) / (1 + cosu); 
  706.   /* dv/dt degrees/day */
  707.   *arp = d->axis * up * DEGTORAD * SIN8(u) * e->ex; 
  708.   /* dr/dt AU/day */
  709.   *azp = *arp * sinv * sini + *ar * vp * DEGTORAD * cosv * sini;    /* dz/dt */
  710.   *alp = vp / cosi * (1 - k8 * k8);
  711.  
  712.   /* now come the disturbations */
  713.  
  714.   switch (planet) {
  715.     REAL8 am, mma, ema, u2;
  716.  
  717.   case EARTH:  
  718.     /* 
  719.     ** earth has some special moon values and a disturbation series due to the
  720.     ** planets. The moon stuff is due to the fact, that the mean elements
  721.     ** give the coordinates of the earth-moon barycenter. By adding the
  722.     ** corrections we effectively reduce to the center of the earth.
  723.     ** We neglect the correction in latitude, which is about 0.5", because
  724.     ** for astrological purposes we want the Sun to have latitude zero.
  725.     */
  726.     am = DEGTORAD * smod8360(el[MOON].lg - e->lg + 180.0); /* degrees */
  727.     mma = DEGTORAD * el[MOON].ma;
  728.     ema = DEGTORAD * e->ma;
  729.     u2 = 2.0 * DEGTORAD * (e->lg - 180.0 - el[MOON].kn); /* 2u' */
  730.     lk = 6.454 * SIN8(am) 
  731.       + 0.013 * SIN8(3.0 * am)
  732.       + 0.177 * SIN8(am + mma)
  733.       - 0.424 * SIN8(am - mma)
  734.       + 0.039 * SIN8(3.0 * am - mma)
  735.       - 0.064 * SIN8(am + ema)
  736.       + 0.172 * SIN8(am - ema)
  737.       - 0.013 * SIN8(am - mma - ema)
  738.       - 0.013 * SIN8(u2);
  739.     rk = 13360 * COS8(am)
  740.       + 30    * COS8(3.0 * am)
  741.       + 370   * COS8(am + mma)
  742.       - 1330  * COS8(am - mma)
  743.       + 80    * COS8(3.0 * am - mma)
  744.       - 140   * COS8(am + ema)
  745.       + 360   * COS8(am - ema)
  746.       - 30    * COS8(am - mma - ema)
  747.       + 30    * COS8(u2);
  748.     /* long periodic term from mars 15g''' - 8g'', Vol 6 p19, p24 */
  749.     lk += 0.202 * SIN8(DEGTORAD * (315.6 + 893.3 * e->tj));
  750.     disturb(earthkor, al, ar, lk, rk, man);
  751.     break;
  752.  
  753.   case MERCURY:  /* only normal disturbation series */
  754.     disturb(mercurykor, al, ar, 0.0, 0.0, man);
  755.     break;
  756.  
  757.   case VENUS:  /* some longperiod terms and normal series */
  758.     lk = (2.761 - 0.22*e->tj) * SIN8(DEGTORAD * (237.24 + 150.27 * e->tj))
  759.     + 0.269 * SIN8(DEGTORAD * (212.2  + 119.05 * e->tj))
  760.     - 0.208 * SIN8(DEGTORAD * (175.8  + 1223.5 * e->tj));
  761.     /* make seconds */
  762.     disturb(venuskor, al, ar, lk, 0.0, man);
  763.     break;
  764.  
  765.   case MARS:  /* only normal disturbation series */
  766.     disturb(marskor, al, ar, 0.0, 0.0, man);
  767.     break;
  768.   }
  769.   return OK;
  770. }
  771.  
  772.  
  773. void disturb(k, al, ar, lk, rk, man)
  774. register struct kor *k;  /* ENDMARK-terminated array of struct kor */
  775. REAL8 *al, /* longitude in degrees, use a pointer to return value */
  776. *ar;       /* radius in AU */
  777. REAL8 lk,  /* longitude correction in SECONDS OF ARC */
  778.            /* function can be called with an lk and rk already
  779.               != 0, but no value is returned */
  780. rk,        /* radius correction in units of 9th place of log r */
  781. man;       /* mean anomaly of planet */
  782. {
  783.   REAL8 arg;
  784.   while (k->j != ENDMARK) {
  785.     arg = k->j * sa[k->k] + k->i * man;
  786.     lk += k->lampl * COS8(DEGTORAD * (k->lphase - arg));
  787.     rk += k->rampl * COS8(DEGTORAD * (k->rphase - arg));
  788.     k++;
  789.   }
  790.   *ar *= EXP10(rk * 1.0E-9);  /* 10^rk */
  791.   *al += lk / 3600.0;
  792. }
  793.  
  794.  
  795. int moon(al, ar, az)  /* return OK or ERR */
  796. REAL8 *al;
  797. REAL8 *ar;
  798. REAL8 *az;
  799. {
  800.   REAL8 a1,a2,a3,a4,a5,a6,a7,a8,a9,c2,c4,arg,b,d,f,dgc,dlm,dpm,dkm,dls;
  801.   REAL8 ca, cb, cd, f_2d, f_4d, g1c,lk,lk1,man,ms,nib,s,sinarg,sinp,sk;
  802.   REAL8 t, tb, t2c, r2rad, i1corr, i2corr, dlid;
  803.   int i;
  804.   struct elements *e;
  805.   struct m45dat   *mp;
  806. #if MOON_TEST_CORR
  807.   struct m5dat    *m5p;
  808. #endif
  809.   e = &el[MOON];
  810.   t = e->tj * 36525;  /* days from epoch 1900 */
  811.  
  812.   /* new format table II, parameters in full rotations of 360 degrees */
  813.   r2rad = 360.0 * DEGTORAD;
  814.   tb  = t * 1e-12;  /* units of 10^12 */
  815.   t2c = t * t * 1e-16;  /* units of 10^16 */
  816.   a1 = SIN8(r2rad * (0.53733431 -  10104982 * tb + 191 * t2c));
  817.   a2 = SIN8(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
  818.   c2 = COS8(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
  819.   a3 = SIN8(r2rad * (0.14222222 +   1536238 * tb));
  820.   a4 = SIN8(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
  821.   c4 = COS8(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
  822.   a5 = SIN8(r2rad * (0.52453688 - 147162675 * tb +  43 * t2c));
  823.   a6 = SIN8(r2rad * (0.84536324 -  11459387 * tb));
  824.   a7 = SIN8(r2rad * (0.23363774 +   1232723 * tb + 191 * t2c));
  825.   a8 = SIN8(r2rad * (0.58750000 +   9050118 * tb));
  826.   a9 = SIN8(r2rad * (0.61043085 -  67718733 * tb));
  827.  
  828.   dlm = 0.84 * a3 + 0.31 * a7 + 14.27 * a1 + 7.261  * a2 + 0.282 * a4 
  829.   + 0.237 * a6;
  830.   dpm = -2.1  * a3 - 2.076  * a2 - 0.840 * a4 - 0.593 * a6;
  831.   dkm = 0.63 * a3 + 95.96 * a2 + 15.58 * a4 + 1.86 * a5;
  832.   dls = -6.4  * a3 - 0.27 * a8 - 1.89  * a6 + 0.20 * a9;
  833.   dgc = (-4.318 * c2 - 0.698 * c4) / 3600.0 / 360.0;  /* in revolutions */
  834.   dgc = (1.000002708 + 139.978 * dgc);  /* in this form used later */
  835.   man = DEGTORAD * (e->ma + (dlm - dpm) / 3600.0); 
  836.   /* man with periodic and secular corr. */
  837.   ms  = DEGTORAD * (el[EARTH].ma + dls / 3600.0);
  838.   f   = DEGTORAD * (e->lg - e->kn + (dlm - dkm) / 3600.0);
  839.   d   = DEGTORAD * (e->lg + 180 - el[EARTH].lg + (dlm - dls) / 3600.0);
  840.  
  841.   lk = lk1 = sk = sinp = nib = g1c = 0;
  842.   i1corr = 1.0 - 6.8320E-8 * t;
  843.   i2corr = dgc * dgc; /* i2 occurs only as -2, 2 */
  844.   for (i = 0, mp = m45; i < NUM_MOON_CORR; i++, mp++) {
  845.     /* arg = mp->i0 * man + mp->i1 * ms + mp->i2 * f + mp->i3 * d; */
  846.     arg = mp->i0 * man;
  847.     arg += mp->i3 * d;
  848.     arg += mp->i2 * f;
  849.     arg += mp->i1 * ms;
  850.     sinarg = SIN8(arg);
  851.     /*
  852.     ** now apply corrections due to changes in constants;
  853.     ** we correct only terms in l' (i1) and F (i2), not in l (i0), because
  854.     ** the latter are < 0.05" 
  855.     ** We don't apply corrections  for cos(arg), i.e. for parallax 
  856.     */
  857.     if (mp->i1 != 0) {  /* i1 can be -2, -1, 0, 1, 2 */
  858.       sinarg *= i1corr;
  859.       if  (mp->i1 == 2 || mp->i1 == -2) 
  860.         sinarg *= i1corr;
  861.     }
  862.     if (mp->i2 != 0)  /* i2 can be -2, 0, 2 */
  863.       sinarg *= i2corr;
  864.     lk += mp->lng * sinarg;
  865.     sk += mp->lat * sinarg;
  866.     sinp += mp->par * COS8 (arg) ;
  867.   }
  868.  
  869. #if MOON_TEST_CORR  /* optionally add more lunar longitudes  */
  870.   for (m5p = m5; m5p->i0 != 99; m5p++) {  /* i0 = 99 is end mark */
  871.     arg = m5p->i0 * man + m5p->i1 * ms + m5p->i2 * f + m5p->i3 * d;
  872.     sinarg = SIN8(arg);
  873.     lk1 += m5p->lng * sinarg;
  874.   }  
  875. #endif
  876.  
  877.   /*
  878.   ** now compute some planetary terms in longitude, list i delta;
  879.   ** we take all > 0.5" and neglect secular terms in the arguments. These
  880.   ** produce phase errors > 10 degrees only after 3000 years.
  881.   */
  882.   dlid =  0.822 * SIN8 (r2rad * (0.32480 - 0.0017125594 * t));
  883.   dlid += 0.307 * SIN8 (r2rad * (0.14905 - 0.0034251187 * t));
  884.   dlid += 0.348 * SIN8 (r2rad * (0.68266 - 0.0006873156 * t));
  885.   dlid += 0.662 * SIN8 (r2rad * (0.65162 + 0.0365724168 * t));
  886.   dlid += 0.643 * SIN8 (r2rad * (0.88098 - 0.0025069941 * t));
  887.   dlid += 1.137 * SIN8 (r2rad * (0.85823 + 0.0364487270 * t));
  888.   dlid += 0.436 * SIN8 (r2rad * (0.71892 + 0.0362179180 * t));
  889.   dlid += 0.327 * SIN8 (r2rad * (0.97639 + 0.0001734910 * t));
  890.  
  891.   /* without nutation */
  892.   *al = smod8360(e->lg + (dlm + lk + lk1 + dlid) / 3600.0);
  893.  
  894.   /* solar Terms in latitude Nibeta */
  895.   f_2d = f - 2.0 * d;
  896.   f_4d = f - 4.0 * d;
  897.   nib += -526.069 * SIN8(                   f_2d);
  898.   nib +=   -3.352 * SIN8(                   f_4d);
  899.   nib +=   44.297 * SIN8( man             + f_2d);
  900.   nib +=   -6.000 * SIN8( man             + f_4d);
  901.   nib +=   20.599 * SIN8(-man             + f   );
  902.   nib +=  -30.598 * SIN8(-man             + f_2d);
  903.   nib +=  -24.649 * SIN8(-2*man           + f   );
  904.   nib +=   -2.000 * SIN8(-2*man           + f_2d);
  905.   nib +=  -22.571 * SIN8(          ms     + f_2d); 
  906.   nib +=   10.985 * SIN8(         -ms     + f_2d); 
  907.  
  908.   /* new gamma1C from 29 Jul 88, all terms > 0.4 " in table III, code 2 */
  909.   g1c += -0.725 * COS8(          d);
  910.   g1c +=  0.601 * COS8(      2 * d);
  911.   g1c +=  0.394 * COS8(      3 * d);
  912.   g1c += -0.445 * COS8(man                   + 4 * d);
  913.   g1c +=  0.455 * COS8(man                   + 1 * d);
  914.   g1c +=  5.679 * COS8(2 * man               - 2 * d);
  915.   g1c += -1.300 * COS8(3 * man                      );
  916.   g1c += -1.302 * COS8(            ms               );
  917.   g1c += -0.416 * COS8(            ms        - 4 * d);
  918.   g1c += -0.740 * COS8(        2 * ms        - 2 * d);
  919.   g1c +=  0.787 * COS8(    man +   ms        + 2 * d);
  920.   g1c +=  0.461 * COS8(    man +   ms               );
  921.   g1c +=  2.056 * COS8(    man +   ms        - 2 * d);
  922.   g1c += -0.471 * COS8(    man +   ms        - 4 * d);
  923.   g1c += -0.443 * COS8(   -man +   ms        + 2 * d);
  924.   g1c +=  0.679 * COS8(   -man +   ms               );
  925.   g1c += -1.540 * COS8(   -man +   ms        - 2 * d);
  926.  
  927.   s =  f + sk / 3600.0 * DEGTORAD;
  928.   ca = 18519.7 + g1c;
  929.   cb = -0.000336992 * ca * dgc * dgc * dgc; 
  930.   cd = ca / 18519.7;
  931.   b = (ca * SIN8(s) * dgc  + cb * SIN8(3.0 * s) + cd * nib) / 3600.0;
  932.  
  933.   /* we neglect the planetary terms in latitude, code 4 in table III */
  934.  
  935.   sinp = (sinp + 3422.451); 
  936.   /*
  937.   ** Improved lunar ephemeris and APAE until ca. 1970 had here
  938.   ** 3422.54 as constant of moon's sine parallax.
  939.   ** The difference can be applied by direct addition of 0.089" to
  940.   ** our parallax results.
  941.   **
  942.   ** To get the radius in A.U. from the sine parallax,
  943.   ** we use 1964 IAU value 8.794" for solar parallax.
  944.   ** sinp is still in seconds of arc.
  945.   ** To calculate moon parallax in " it would be:
  946.   ** p = sinp (1  + sinp * sinp * 3.917405E-12)
  947.   ** based on the formula p = sinp + 1/6 sinp^3
  948.   ** and taking into account the conversion of " to radians.
  949.   ** The semidiameter of the moon is: (Expl.Suppl. 61, p 109)
  950.   ** s = 0.0796 + 0.272446 * p
  951.   */
  952.  
  953.   *ar = 8.794 / sinp;
  954.   *az = *ar * SIN8(DEGTORAD * b);
  955.   return OK;
  956. }
  957.  
  958.  
  959. /* solution of the Kepler equation, return rad */
  960. /* t = mean anomaly in degrees                 */
  961. /* ex = excentricity of orbit                  */
  962. /* err = maximum error in degrees              */
  963.  
  964. REAL8 fnu(t, ex, err)
  965. REAL8 t;
  966. REAL8 ex;
  967. REAL8 err;
  968. {
  969.   REAL8 u0, delta;
  970.  
  971.   t *= DEGTORAD;
  972.   u0 = t;
  973.   err *= DEGTORAD;
  974.   delta = 1;
  975.   while (ABS8(delta) >= err) {
  976.     delta = (t + ex * SIN8(u0) - u0) / (1 - ex * COS8(u0));
  977.     u0 += delta;  
  978.   }
  979.   return u0;  
  980. }
  981.  
  982.  
  983. /*
  984. ** outer_hel()
  985. ** Computes the position of Jupiter, Saturn, Uranus, Neptune, Pluto and
  986. ** Chiron by reading our stored ephemeris in steps of 80 (!) days and
  987. ** applying a high order interpolation to it. The interpolation errors are
  988. ** less than 0.01" seconds or arc.
  989. ** The stored ephemeris is  packed in a special format consisting of
  990. ** 32 bit numbers; it has been created on the Astrodienst Unix system
  991. ** by numerical integration with routines provided originally by Marc
  992. ** Pottenger, USA, which we improved for better long term precision.
  993. ** Because the Unix system uses a different byte order than the MSDOS
  994. ** systems, the bytes must be reordered for MSDOS after reading from
  995. ** the binary files. 
  996. **
  997. ** outer_hel() takes the same parameters as hel().
  998. ** It returns the same type of values.
  999. **
  1000. ** The access to the ephemeris files is done in the functions chi_file_posit()
  1001. ** and lrz_file_posit().
  1002. */
  1003.  
  1004. int outer_hel(planet, jd_ad, al, ar, az, alp, arp, azp)
  1005. int planet;
  1006. REAL8 jd_ad;  /* jd_ad Astrodienst relative Julian ephemeris time */
  1007. REAL8 *al;
  1008. REAL8 *ar;
  1009. REAL8 *az;
  1010. REAL8 *alp;
  1011. REAL8 *arp;
  1012. REAL8 *azp;
  1013. {
  1014.   static FILE *outerfp = NULL, *chironfp = NULL;
  1015.   static double last_j0_outer = HUGE8;
  1016.   static double last_j0_chiron = HUGE8;
  1017.   static long  icoord[6][5][3], chicoord[6][3];
  1018.   REAL8 j0, jd, jfrac;
  1019.   REAL8 l[6], r[6], z[6];
  1020.   int n, order, p;
  1021.  
  1022.   if (planet < JUPITER || planet > PLUTO && planet != CHIRON)
  1023.     return ERR;
  1024.   jd = jd_ad + JUL_OFFSET;
  1025.   j0 = RFloor((jd - 0.5) / EPHE_STEP) * EPHE_STEP + 0.5;
  1026.   jfrac = (jd - j0) / EPHE_STEP;
  1027.   if (planet == CHIRON) {
  1028.     if (last_j0_chiron != j0) {
  1029.       for (n = 0; n < 6; n++) { /* read 6 days */
  1030.         jd = j0 + (n - 2) * EPHE_STEP;
  1031.         if (chi_file_posit(jd, &chironfp) != OK)
  1032.           return (ERR);
  1033.         fread(&chicoord[n][0], sizeof(long), 3, chironfp); 
  1034.         longreorder((UCHAR *)&chicoord[n][0], 3*4);
  1035.       }
  1036.       last_j0_chiron = j0;
  1037.     }
  1038.     for (n = 0; n < 6; n++) {
  1039.       l[n] = chicoord[n][0] / DEG2MSEC;
  1040.       r[n] = chicoord[n][1] / AU2INT;
  1041.       z[n] = chicoord[n][2] / AU2INT;
  1042.     }
  1043.   } else {  /* an outerplanet */
  1044.     if (last_j0_outer != j0) {  /* read all 5 planets for 6 steps */
  1045.       for (n = 0; n < 6; n++) { 
  1046.         jd = j0 + (n - 2) * EPHE_STEP;
  1047.         if (lrz_file_posit(jd, &outerfp) != OK)
  1048.           return (ERR);
  1049.         fread(&icoord[n][0][0], sizeof(long), 15, outerfp); 
  1050.         longreorder((UCHAR *)&icoord[n][0][0], 15*4);
  1051.       }
  1052.       last_j0_outer = j0;
  1053.     }
  1054.     p = planet - JUPITER;
  1055.     for (n = 0; n < 6; n++) {
  1056.       l[n] = icoord[n][p][0] / DEG2MSEC;
  1057.       r[n] = icoord[n][p][1] / AU2INT;
  1058.       z[n] = icoord[n][p][2] / AU2INT;
  1059.     }
  1060.   }
  1061.   if (planet > SATURN)
  1062.     order = 3;
  1063.   else
  1064.     order = 5;
  1065.   inpolq(2, order, jfrac, l, al, alp);
  1066.   *alp /= EPHE_STEP;
  1067.   inpolq(2, order, jfrac, r, ar, arp);
  1068.   *arp /= EPHE_STEP;
  1069.   inpolq(2, order, jfrac, z, az, azp);
  1070.   *azp /= EPHE_STEP;
  1071.   return OK;
  1072. }
  1073.  
  1074.  
  1075. /*
  1076. ** quicker Everett interpolation, after Pottenger
  1077. ** version 9 Jul 1988 by Alois Treindl
  1078. ** return OK or ERR.
  1079. */
  1080.  
  1081. int inpolq(n, o, p, x, axu, adxu)
  1082. int n,     /* interpolate between x[n] and x[n-1], at argument n+p */
  1083. o;         /* order of interpolation, maximum 5 */
  1084. double p,  /* argument , intervall [0..1] */
  1085. x[],       /* array of function values, x[n-o]..x[n+o] must exist */
  1086. *axu,      /* pointer for storage of result */
  1087. *adxu;     /* pointer for storage of dx/dt  */
  1088. {
  1089.   static double q, q2, q3, q4, q5, p2, p3, p4, p5, u, u0, u1, u2;
  1090.   static double lastp = 9999;
  1091.   double dm2, dm1, d0, dp1, dp2,
  1092.     d2m1, d20, d2p1, d2p2, d30, d3p1, d3p2, d4p1, d4p2;
  1093.   double offset = 0.0;
  1094.  
  1095.   if (lastp != p) {
  1096.     q  = 1.0-p;
  1097.     q2 = q*q;
  1098.     q3 = (q+1.0)*q*(q-1.0)/6.0;       /* q - 1 over 3; u5 */
  1099.     p2 = p*p;
  1100.     p3 = (p+1.0)*p*(p-1.0)/6.0;       /* p - 1 over 3; u8 */
  1101.     u  = (3.0*p2-1.0)/6.0;
  1102.     u0 = (3.0*q2-1.0)/6.0;
  1103.     q4 = q2*q2;                       /* f5 */
  1104.     p4 = p2*p2;                       /* f4 */
  1105.     u1 = (5.0*p4-15.0*p2+4.0)/120.0;  /* u1 */
  1106.     u2 = (5.0*q4-15.0*q2+4.0)/120.0;  /* u2 */
  1107.     q5 = q3*(q+2.0)*(q-2.0)/20.0;     /* q - 2 over 5; u6 */
  1108.     p5 = (p+2.0)*p3*(p-2.0)/20.0;     /* p - 2 over 5; u9 */
  1109.     lastp = p;
  1110.   }
  1111.   dm1 = x[n] - x[n-1];
  1112.   if (dm1 > 180.0)
  1113.     dm1 -= 360.0;
  1114.   if (dm1 < -180.0)
  1115.     dm1 += 360.0;
  1116.   d0 = x[n+1] - x[n];
  1117.   if (d0 > 180.0) {
  1118.     d0 -= 360.0;
  1119.     offset = 360.0;
  1120.   }
  1121.   if (d0 < -180.0) {
  1122.     d0 += 360.0;
  1123.     offset = -360.0;
  1124.   }
  1125.   dp1 = x[n+2] - x[n+1];
  1126.   if (dp1 > 180.0)
  1127.     dp1 -= 360.0;
  1128.   if (dp1 < -180.0)
  1129.     dp1 += 360.0;
  1130.   d20  = d0 - dm1;    /* f8 */
  1131.   d2p1 = dp1 - d0;    /* f9 */
  1132.  
  1133.   /* Everett interpolation 3rd order */
  1134.  
  1135.   *axu = q*(x[n] + offset) + q3*d20 + p*x[n+1] + p3*d2p1;
  1136.   *adxu = d0 + u*d2p1 - u0*d20;
  1137.   if (o > 3) {    /* 5th order */
  1138.     dm2 = x[n-1] - x[n-2];
  1139.     if (dm2 > 180.0)
  1140.       dm2 -= 360.0;
  1141.     if (dm2 < -180.0)
  1142.       dm2 += 360.0;
  1143.     dp2 = x[n+3] - x[n+2];
  1144.     if (dp2 > 180.0)
  1145.       dp2 -= 360.0;
  1146.     if (dp2 < -180.0)
  1147.       dp2 += 360.0;
  1148.     d2m1 = dm1 - dm2;
  1149.     d2p2 = dp2 - dp1;
  1150.     d30  = d20 - d2m1;
  1151.     d3p1 = d2p1 - d20;
  1152.     d3p2 = d2p2 - d2p1;
  1153.     d4p1 = d3p1 - d30;     /* f7 */
  1154.     d4p2 = d3p2 - d3p1;    /* f */
  1155.     *axu  += p5*d4p2 + q5*d4p1;
  1156.     *adxu += u1*d4p2 - u2*d4p1;
  1157.   }
  1158.   return OK;
  1159. }
  1160.  
  1161.  
  1162. /*
  1163. ** position lrz file at proper position for julian date jd; 
  1164. ** Return OK or ERR.  Version for outer planets.
  1165. ** The path where the ephemeris files are looked for is defined
  1166. ** by ephe_path.
  1167. */
  1168.  
  1169. int lrz_file_posit(jd, lrzfpp)
  1170. double jd;     /* full Julian day number, not Astrodienst relative */
  1171. FILE **lrzfpp; /* pointer to file pointer; this function
  1172.                   opens or closes the ephemeris file, and caller
  1173.                   should keep it open while using it. The caller
  1174.                   should close it when he is finished with using
  1175.                   the placalc() package. */
  1176. {
  1177.   int filenr;
  1178.   long posit, jlong;
  1179.   static char fname[80];
  1180.   static int open_lrznr = -10000; /* local memory to remember whether
  1181.     an already open file is the one with
  1182.     the correct number for this date */
  1183.  
  1184.   jlong = (long)RFloor(jd); 
  1185.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);   
  1186.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1187.     filenr--;
  1188.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1189.   posit = (posit / (int)EPHE_STEP) * EPHE_OUTER_BSIZE;
  1190.   if (*lrzfpp == NULL || open_lrznr != filenr) { /* no or wrong open file */
  1191.     open_lrznr = -10000;
  1192.     sprintf(fname, "%s%s%d", EPHE_OUTER, filenr < 0 ? "M" : "", abs(filenr));
  1193.     if (*lrzfpp != NULL)
  1194.       fclose(*lrzfpp);
  1195.     *lrzfpp = FileOpen(fname, 2);
  1196.     if (*lrzfpp == NULL) {
  1197.       fprintf(stderr, "Placalc: File %s does not exist.\n", fname);
  1198.       return (ERR); 
  1199.     }
  1200.     open_lrznr = filenr;
  1201.   }
  1202.   if (fseek(*lrzfpp, posit, 0) == 0)
  1203.     return (OK);
  1204.   fprintf(stderr, "Placalc: Seek error file %s position %ld\n", fname, posit);
  1205.   return ERR; /* this fseek error occurs only with incomplete files */
  1206. }
  1207.  
  1208.  
  1209. /*
  1210. ** position chiron file at proper position for julian date jd; 
  1211. ** Return OK or ERR. Version for Chiron.
  1212. ** Sister function to lrz_file_posit().
  1213. */
  1214.  
  1215. int chi_file_posit(jd, lrzfpp)
  1216. double jd;  /* full Julian day number, not Astrodienst relative */
  1217. FILE **lrzfpp; /* pointer to file pointer; this function
  1218. opens or closes the ephemeris file, and caller
  1219. should keep it open while using it */
  1220. {
  1221.   int filenr;
  1222.   long posit, jlong;
  1223.   char fname[80];
  1224.   static int open_lrznr = -10000; /* local memory to remember whether
  1225.     an already open file is the one with
  1226.     the correct number for this date */
  1227.  
  1228.   jlong = (long)RFloor(jd); 
  1229.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);  
  1230.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1231.     filenr--;
  1232.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1233.   posit = (posit / (int)EPHE_STEP) * EPHE_CHIRON_BSIZE;
  1234.   if (*lrzfpp == NULL || open_lrznr != filenr) { /* no or wrong open file */
  1235.     open_lrznr = -10000;
  1236.     sprintf(fname, "%s%s%d", EPHE_CHIRON, filenr < 0 ? "M" : "", abs(filenr));
  1237.     if (*lrzfpp != NULL)
  1238.       fclose(*lrzfpp);
  1239.     *lrzfpp = FileOpen(fname, 2);
  1240.     if (*lrzfpp == NULL) {
  1241.       fprintf(stderr, "Placalc: File %s does not exist.\n", fname);
  1242.       return ERR; 
  1243.     }
  1244.     open_lrznr = filenr;
  1245.   }
  1246.   if (fseek (*lrzfpp, posit, 0) == 0)
  1247.     return OK;
  1248.   fprintf(stderr, "Placalc: Seek error file %s position %ld\n", fname, posit);
  1249.   return ERR; /* this fseek error occurs only with incomplete files */
  1250. }
  1251.  
  1252.  
  1253. /* x MOD 360.0, so that x in 0..360 */
  1254.  
  1255. REAL8 smod8360(x)
  1256. REAL8 x;
  1257. {
  1258.   while (x >= 360.0)
  1259.     x -= 360.0;
  1260.   while (x < 0.0)
  1261.     x += 360.0;
  1262.   return x;
  1263. }
  1264.  
  1265.  
  1266. /* x MOD 360.0, so that x in 0..360 */
  1267.  
  1268. REAL8 mod8360(x)
  1269. REAL8 x;
  1270. {
  1271.   if (x >= 0 && x < 360.0)
  1272.     return x;
  1273.   return x - 360.0 * RFloor(x / 360.0);
  1274. }
  1275.  
  1276.  
  1277. /* a - b on a 360 degree circle, result -180..180 */
  1278.  
  1279. REAL8 diff8360(a, b) 
  1280. REAL8 a;
  1281. REAL8 b;
  1282. {
  1283.   REAL8 d;
  1284.  
  1285.   d = a - b;
  1286.   if (d >= 180.0)
  1287.     return d - 360.0;
  1288.   if (d < -180.0)
  1289.     return d + 360.0;
  1290.   return d;
  1291. }
  1292.  
  1293.  
  1294. REAL8 test_near_zero(x)
  1295. REAL8 x;
  1296. {
  1297.   if (ABS8(x) >= NEAR_ZERO)
  1298.     return x;
  1299.   if (x < 0)
  1300.     return -NEAR_ZERO; 
  1301.   return NEAR_ZERO;
  1302. }
  1303.  
  1304.  
  1305. /*
  1306. ** p points to memory filled with long values; for
  1307. ** each of the values the seqeuence of the four bytes
  1308. ** has to be reversed, to translate HP-UX and VAX
  1309. ** ordering to MSDOS/Turboc ordering
  1310. */
  1311.  
  1312. void longreorder(p, n) 
  1313. UCHAR *p;
  1314. int n;
  1315. {
  1316.   int i;
  1317.   unsigned char c0, c1, c2, c3;
  1318.   static int orderinit = 0;
  1319.   unsigned short test;
  1320.  
  1321.   if (!orderinit) {
  1322.     test = 0x0001;
  1323.     orderinit = (*(unsigned char *)(&test)) ? 1 : -1;
  1324.   }
  1325.   if (orderinit < 0)
  1326.     return;
  1327.   for (i = 0; i < n; i += 4, p += 4) {
  1328.     c0 = *p;
  1329.     c1 = *(p + 1);
  1330.     c2 = *(p + 2);
  1331.     c3 = *(p + 3);
  1332.     *p = c3;
  1333.     *(p + 1) = c2;
  1334.     *(p + 2) = c1;
  1335.     *(p + 3) = c0;
  1336.   }
  1337. }
  1338. #endif /* PLACALC */
  1339.  
  1340. /* placalc.c */
  1341.